<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>设置景点价格/库存</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('style.css,base.css,base2.css,base_new.css,calendar-price-jquery.min.css'); }
    {Common::getScript("jquery.validate.js,choose.js,product_add.js,jquery.upload.js,DatePicker/WdatePicker.js,template.js,xmselect/xmselect.js")}
    {Common::css_plugin('ferry_edit.css','ferry')}
    {php echo Common::js_plugin('jquery.datetimepicker.full.js','ferry');}
    {php echo Common::css_plugin('jquery.datetimepicker.css','ferry');}
    {php echo Common::getScript("laydate/laydate.js")}
    <style>
        .dialog-body {
            height: 100%;
            position: relative;
        }

        .dialog-body .save-con {
            position: fixed;
            bottom: 0;
            background: #fff;
            width: 100%;
            text-align: right;
            border-top: solid 1px #e5e5e5;
            padding-top: 10px;
            z-index: 999;
        }

        .dialog-body .product-add-div {
            padding-bottom: 50px;
        }

        .rule-item {
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .rule-item .rule-item-hd {
            width: 100px;
            text-align: right;
        }
        .suit-block th,.suit-block td{
            border:1px solid #DBDBDB
        }
        xm-select * {
            font-size:12px;
        }
        .suit-edit-block .suit-edit-tb{
            padding: 5px;
            box-sizing: border-box;
        }

        .suit-setting {
            position: relative;
        }
        .suit-setting-con {
            background: #fff;
            min-width: 100px;
            border-radius: 2px;
            box-shadow: 0 2px 4px #BBB;
            padding: 10px;
            top: 50px;
            left: 0;
            position: absolute;
            display: none;
        }
        .suit-setting-btns{
            display:flex;
            justify-items: center;
            justify-content: space-between;
        }

        #date_type_date{
            display:flex;
            justify-items: center;
            justify-content: flex-start;
        }
        #date_type_calendar_list{
            width: 540px;
            height: 278px;
            border: 1px solid #dbdbdb;
            margin-left: 20px;
            box-shadow: 0 0 5px #dbdbdb;
            border-radius: 4px;
            overflow-y: auto;
            box-sizing: border-box;
            padding: 10px;
        }
        #date_type_calendar_list .item{
            padding: 4px 20px 4px 10px;
            background:#F1F1F1;
            display: inline-block;
            border-radius: 2px;
            margin-right: 4px;
            position: relative;
            margin-bottom: 4px;
        }
        #date_type_calendar_list .item .del{
            display: inline-block;
            width: 10px;
            height: 10px;
            cursor: pointer;
            opacity: .6;
            position: absolute;
            right: 4px;
            top: 10px;
            background: url(/newtravel/public/images/tab-bar-closed-icon.png) no-repeat 0 0;
        }
    </style>
</head>
<body>
<div class="dialog-body">
    <div class="product-add-div pb-20 mb-20">
        <form id="suit_form">
            <input type="hidden" name="ferry_id" value="{$ferry_id}"/>
            <input type="hidden" name="date_type_week_start" value=""/>
            <input type="hidden" name="date_type_week_end" value=""/>
            <ul class="info-item-block" style="padding-top:0">
                <li>
                    <div class="item-hd"><i class="c-red">*</i>选择套餐：</div>
                    <div class="item-bd">
                        <div id="calendar_price_suit" class="w300 fl mr-10"></div>
                    </div>
                </li>
                <li>
                    <div class="item-hd"><i class="c-red">*</i>选择时间周期：</div>
                    <div class="item-bd">
                        <div>
                            <label class="radio-label mr-10">
                                <input type="radio" checked name="date_type" value="week" />按日期范围设置
                            </label>
                            <label class="radio-label">
                                <input type="radio" name="date_type" value="date" />按日期设置
                            </label>
                        </div>
                        <div>
                            <div id="date_type_week">
                                <div>
                                    <label class="radio-label select-none mr-15">
                                        <input type="text" id="price_date_range" placeholder="开始日期 - 结束日期" name="date_range" value="" class="input-text w200" autocomplete="off">
                                    </label>
                                </div>
                                <div>
                                    <label class="check-label mr-10">
                                        <input type="checkbox" checked name="day_type[]" value="0" />每天
                                    </label>
                                    <label class="check-label mr-10">
                                        <input class="week-select" type="checkbox" name="day_type[]" value="7" />每周日
                                    </label>
                                    <label class="check-label mr-10">
                                        <input class="week-select" type="checkbox" name="day_type[]" value="1" />每周一
                                    </label>
                                    <label class="check-label mr-10">
                                        <input class="week-select" type="checkbox" name="day_type[]" value="2" />每周二
                                    </label>
                                    <label class="check-label mr-10">
                                        <input class="week-select" type="checkbox" name="day_type[]" value="3" />每周三
                                    </label>
                                    <label class="check-label mr-10">
                                        <input class="week-select" type="checkbox" name="day_type[]" value="4" />每周四
                                    </label>
                                    <label class="check-label mr-10">
                                        <input class="week-select" type="checkbox" name="day_type[]" value="5" />每周五
                                    </label>
                                    <label class="check-label mr-10">
                                        <input class="week-select" type="checkbox" name="day_type[]" value="6" />每周六
                                    </label>
                                </div>
                            </div>
                            <div id="date_type_date" style="display:none">
                                <div id="date_type_calendar"></div>
                                <div id="date_type_calendar_list">

                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <div class="item-hd"><i class="c-red">*</i>维护价格库存：</div>
                    <div class="item-bd pr-10">
                        {if count($skus_settlement_list['profit_type'])>0}
                        <span class="item-text">结算价结算类型</span>
                        <table class="suit-edit-block suit-block" >
                            <thead class="suit_thead">
                            <tr class="suit-edit-th">
                                <th class="text-c">
                                    门票名称
                                </th>
                                {if !$settlement_type_single || $settlement_type == 1}
                                <th class="user-click text-c suit-setting">
                                    底价<span class="c-link ml-5 suit-setting-tool">设置</span>
                                    <div class="suit-setting-con">
                                        <div class="text-l mb-5">批量设置底价</div>
                                        <div>
                                            <label>
                                                <input name="all_basic_price" type="number" value="" maxlength="9" placeholder="底价" class="input-text"/>
                                            </label>
                                        </div>
                                        <div class="mt-10 suit-setting-btns">
                                            <span class="btn btn-primary-outline size-S reset_suit_filter" data-index="basic_price">取消</span>
                                            <span class="btn btn-primary size-S fr confirm_suit_filter" data-index="basic_price">确定</span>
                                        </div>
                                    </div>
                                </th>
                                <th class="user-click text-c suit-setting">
                                    利润<span class="c-link ml-5 suit-setting-tool">设置</span>
                                    <div class="suit-setting-con" data-index="profit">
                                        <div class="text-l mb-5">批量设置利润</div>
                                        <div>
                                            <label>
                                                <input name="all_profit" type="number" value="" maxlength="9" placeholder="利润" class="input-text"/>
                                            </label>
                                        </div>
                                        <div class="mt-10 suit-setting-btns">
                                            <span class="btn btn-primary-outline size-S reset_suit_filter" data-index="profit">取消</span>
                                            <span class="btn btn-primary size-S fr confirm_suit_filter" data-index="profit">确定</span>
                                        </div>
                                    </div>
                                </th>
                                {/if}
                                <th class="user-click text-c suit-setting">
                                    库存模式<span class="c-link ml-5 suit-setting-tool">设置</span>
                                    <div class="suit-setting-con" data-index="stock_mod">
                                        <div class="text-l mb-5">批量库存模式</div>
                                        <div>
                                            <label class="select-box">
                                                <select class="select" data-id="{{value.id}}" name="all_stock_mod">
                                                    <option value="1">日库存</option>
                                                    <option value="2">无限库存</option>
                                                    <option value="3">总库存</option>
                                                </select>
                                            </label>
                                        </div>
                                        <div class="mt-10 suit-setting-btns">
                                            <span class="btn btn-primary-outline size-S reset_suit_filter" data-index="stock_mod">取消</span>
                                            <span class="btn btn-primary size-S fr confirm_suit_filter" data-index="stock_mod">确定</span>
                                        </div>
                                    </div>
                                </th>
                                <th class="text-c">
                                    库存数量
                                </th>
                                <th class="text-c">
                                    预警库存数量
                                </th>
                            </tr>
                            </thead>
                            <tbody class="suit_tbody">
                                {loop $skus_settlement_list['profit_type'] $sku}
                                <tr id="sku_{$sku['ticket_id']}_{$sku['sku_id']}" class="show-sku">
                                    <td class="text-c suit-edit-tb">
                                        {$sku['title']}
                                        <input type="hidden" name="settlement_type[{$sku['ticket_id']}_{$sku['sku_id']}]" value="{$sku['settlement_type']}" />
                                    </td>

                                    {if !$settlement_type_single || $settlement_type == 1}
                                    <td class="text-c suit-edit-tb">
                                        {if $sku['settlement_type'] == 1}
                                        <label>
                                            <input name="basic_price[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="底价" class="input-text"/>
                                        </label>
                                        {else}
                                        --
                                        {/if}
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        {if $sku['settlement_type'] == 1}
                                        <label>
                                            <input name="profit[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="利润" class="input-text"/>
                                        </label>
                                        {else}
                                        --
                                        {/if}
                                    </td>
                                    {/if}
                                    <td class="text-c suit-edit-tb">
                                        <label class="select-box">
                                            <select class="select" data-id="{$sku['ticket_id']}_{$sku['sku_id']}" name="stock_mod[{$sku['ticket_id']}_{$sku['sku_id']}]">
                                                <option value="1">日库存</option>
                                                <option value="2">无限库存</option>
                                                <option value="3">总库存</option>
                                            </select>
                                        </label>
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        <label class="stock-setting-1-{$sku['ticket_id']}_{$sku['sku_id']}">
                                            <input name="stock[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="库存数量" class="input-text"/>
                                        </label>

                                        <span class="stock-setting-2-{$sku['ticket_id']}_{$sku['sku_id']} hide">无需维护</span>

                                        <label class="total-stock-3-{$sku['ticket_id']}_{$sku['sku_id']} hide">
                                            <input name="total_stock[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="总库存" class="input-text"/>
                                        </label>
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        <label class="stock-mod-1-{$sku['ticket_id']}_{$sku['sku_id']}">
                                            <input name="warning_stock[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="预警库存数量" class="input-text"/>
                                        </label>

                                        <span class="stock-mod-2-{$sku['ticket_id']}_{$sku['sku_id']} hide">无需维护</span>
                                        <span class="stock-setting-3-{$sku['ticket_id']}_{$sku['sku_id']} hide">无需维护</span>
                                    </td>
                                </tr>
                                {/loop}
                            </tbody>
                        </table>
                        {/if}
                        {if count($skus_settlement_list['commission_type'])>0}
                        <span class="item-text mt-10">销售价结算类型</span>
                        <table class="suit-edit-block suit-block" >
                            <thead class="suit_thead">
                            <tr class="suit-edit-th">
                                <th class="text-c" width="17%">
                                    门票名称
                                </th>
                                <th class="user-click text-c suit-setting">
                                    卖价
                                    {if $sku['settlement_type'] == 1}
                                    <span class="c-link ml-5 suit-setting-tool">设置</span>
                                    <div class="suit-setting-con" data-index="price">
                                        <div class="text-l mb-5">批量设置卖价</div>
                                        <div>
                                            <label>
                                                <input name="all_basic_price" type="number" value="" maxlength="9" placeholder="卖价" class="input-text"/>
                                            </label>
                                        </div>
                                        <div class="mt-10 suit-setting-btns">
                                            <span class="btn btn-primary-outline size-S reset_suit_filter" data-index="price">取消</span>
                                            <span class="btn btn-primary size-S fr confirm_suit_filter" data-index="price">确定</span>
                                        </div>
                                    </div>
                                    {/if}
                                </th>
                                <th class="user-click text-c suit-setting">
                                    抽佣方式
                                </th>
                                <th class="user-click text-c suit-setting" width="13%">
                                    比例/金额
                                </th>
                                <th class="user-click text-c suit-setting">
                                    库存模式<span class="c-link ml-5 suit-setting-tool">设置</span>
                                    <div class="suit-setting-con" data-index="stock_mod">
                                        <div class="text-l mb-5">批量库存模式</div>
                                        <div>
                                            <label class="select-box">
                                                <select class="select" data-id="{{value.id}}" name="all_stock_mod">
                                                    <option value="1">日库存</option>
                                                    <option value="2">无限库存</option>
                                                    <option value="3">总库存</option>
                                                </select>
                                            </label>
                                        </div>
                                        <div class="mt-10 suit-setting-btns">
                                            <span class="btn btn-primary-outline size-S reset_suit_filter" data-index="stock_mod">取消</span>
                                            <span class="btn btn-primary size-S fr confirm_suit_filter" data-index="stock_mod">确定</span>
                                        </div>
                                    </div>
                                </th>
                                <th class="text-c">
                                    库存数量
                                </th>
                                <th class="text-c">
                                    预警库存数量
                                </th>
                            </tr>
                            </thead>
                            <tbody class="suit_tbody commission-tbody">
                                {loop $skus_settlement_list['commission_type'] $sku}
                                <tr id="sku_{$sku['ticket_id']}_{$sku['sku_id']}" class="show-sku">
                                    <td class="text-c suit-edit-tb">
                                        {$sku['title']}
                                        <input type="hidden" name="settlement_type[{$sku['ticket_id']}_{$sku['sku_id']}]" value="{$sku['settlement_type']}" />
                                    </td>
                                    <td class="text-c suit-edit-tb sale-price">
                                        <label>
                                            <input name="basic_price[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="卖价" class="input-text price"/>
                                        </label>
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        <select name="settlement_key[{$sku['ticket_id']}_{$sku['sku_id']}]" class="select settlement_key" onchange="settings_switch($(this),{$sku['ticket_id']},{$sku['sku_id']})">
                                            <option value="1">百分比抽佣</option>
                                            <option value="2">固定金额抽佣</option>
                                        </select>
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        <input type="text" onkeyup="number_constraint($(this),{$sku['ticket_id']},{$sku['sku_id']})" placeholder="比例0-100" name="settlement_val[{$sku['ticket_id']}_{$sku['sku_id']}]" class="input-text w100 settlement_val">
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        <label class="select-box">
                                            <select class="select" data-id="{$sku['ticket_id']}_{$sku['sku_id']}" name="stock_mod[{$sku['ticket_id']}_{$sku['sku_id']}]">
                                                <option value="1">日库存</option>
                                                <option value="2">无限库存</option>
                                                <option value="3">总库存</option>
                                            </select>
                                        </label>
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        <label class="stock-setting-1-{$sku['ticket_id']}_{$sku['sku_id']}">
                                            <input name="stock[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="库存数量" class="input-text"/>
                                        </label>

                                        <span class="stock-setting-2-{$sku['ticket_id']}_{$sku['sku_id']} hide">无需维护</span>

                                        <label class="total-stock-3-{$sku['ticket_id']}_{$sku['sku_id']} hide">
                                            <input name="total_stock[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="总库存" class="input-text"/>
                                        </label>
                                    </td>
                                    <td class="text-c suit-edit-tb">
                                        <label class="stock-mod-1-{$sku['ticket_id']}_{$sku['sku_id']}">
                                            <input name="warning_stock[{$sku['ticket_id']}_{$sku['sku_id']}]" type="number" value="" maxlength="9" placeholder="预警库存数量" class="input-text"/>
                                        </label>

                                        <span class="stock-mod-2-{$sku['ticket_id']}_{$sku['sku_id']} hide">无需维护</span>
                                        <span class="stock-setting-3-{$sku['ticket_id']}_{$sku['sku_id']} hide">无需维护</span>
                                    </td>
                                </tr>
                                {/loop}
                            </tbody>
                        </table>
                        {/if}
                    </div>
                </li>
            </ul>
        </form>
    </div>

    <div class="save-con">
        <a href="javascript:;" class="btn btn-grey size-M mr-10" id="cancel-btn">取消</a>
        <a href="javascript:;" class="btn btn-primary size-M mr-30" id="confirm_btn">保存</a>
    </div>
</div>

<!--日期模板-->
<script type="text/html" id="date_type_date_template">
    {{each data as value i}}
    <div class="item">
        {{value}}
        <input value="{{value}}" name="date_type_date[]" type="hidden" />
        <span class="del" onclick="$(this).parent().remove(); delDateSelect()"></span>
    </div>
    {{/each}}
</script>
</body>
<script type="text/javascript">
    var suitTbody = $(".suit_tbody"),
        suitThead = $(".suit_thead");

    // 选择时间周期 ------------
    $("input[name=date_type]").on('change', function (e) {
        var value = $(this).val(),
            ids = "#date_type_" + value;
        $(ids).show();
        $(ids).siblings().hide();
    })

    // 按日期设置
    laydate.render({
        elem: '#date_type_calendar'
        , position: 'static'
        , value: ''
        , min: '{date("Y-m-d")}'
        , max: '{date("Y-m-d", strtotime("+1 year"))}'
        , theme: '{Common::get_back_color()}'
        , showBottom: false
        , change: function () {
            delDateSelect()
        }
        , done: function (value) {
            var values = getWeekTypeDateValue()

            var index = values.findIndex(function (val) {
                return val === value
            });

            if (index != -1) {
                values.splice(index, 1);
            } else {
                values.push(value);
            }

            dateSelect(values);

            var html = template("date_type_date_template", {data: values});
            $("#date_type_calendar_list").html(html);
        },
    });

    // 删除日期选择
    function delDateSelect() {
        var values = getWeekTypeDateValue()
        dateSelect(values);
    }

    function getWeekTypeDateValue() {
        var values = [],
            dateItems = $("#date_type_calendar_list").find('input[name*=date_type_date]');
        dateItems.each(function (index, item) {
            values = values.concat(item.value);
        })

        return values;
    }

    function removeAll() {
        document.querySelectorAll('#date_type_calendar td[lay-ymd].layui-this').forEach(function (dom) {
            dom.classList.remove('layui-this');
        });
    }

    function dateSelect(values) {
        removeAll()

        values.forEach(function (val) {
            var dom = document.querySelector('#date_type_calendar td[lay-ymd="' + val.replace(/-0([1-9])/g, '-$1') + '"]');
            dom && dom.classList.add('layui-this');
        });
    }

    // 选择时间周期 ------------

    // 星期选择
    $("input[name*=day_type]").click(function () {
        var value = $(this).val();
        value = parseInt(value);
        if (value === 0) {
            $(this).prop('checked', true);
            $(".week-select").prop('checked', false);
        } else {
            var weekObj = $(".week-select:checked");
            if (weekObj.length === 7) {
                $("input[name*=day_type]").eq(0).prop('checked', true)
                $(".week-select").prop('checked', false);
            } else if (weekObj.length === 0) {
                ST.Util.showFailureMsg('至少选择一个日期');
                $(this).prop('checked', true);
            } else {
                $("input[name*=day_type]").eq(0).prop('checked', false)
            }
        }
    })

    // 日历初始化 - 按星期设置------------
    var suitSelect = xmSelect.render({
        el: '#calendar_price_suit',
        size: 'mini',
        tips: '请选择套餐',
        height: '200px',
        name: 'suit_list',
        toolbar: {showIcon: false,show: true},
        prop: {
            name: 'title',
            value: 'tag',
        },
        filterable: true,
        theme: {
            color: '{Common::get_back_color()}',
        },
        data: function () {
            return {json_encode($skus)}
        },
        on: function (res) {
            suitSelect.closed();
            var data = res.arr;
            if (data.length > 0) {
                suitTbody.find('tr').hide();
                suitTbody.find('tr').removeClass('show-sku');
                $.each(data, function (key, val) {
                    var ids = '#sku_' + val.ticket_id + '_' + val.sku_id;
                    $(ids).show();
                    $(ids).addClass('show-sku');
                })
            } else {
                suitTbody.find('tr').show();
                suitTbody.find('tr').addClass('show-sku');
            }
        }
    });

    // 日期范围初始化
    var laydate = laydate.render({
        elem: '#price_date_range'
        ,range: true
        ,show: true //直接显示
        ,min: '{date("Y-m-d")}'
        ,max: '{date("Y-m-d", strtotime("+1 year"))}'
        ,theme: '{Common::get_back_color()}'
        ,btns: ['confirm']
        ,calendar: true
        ,ready:function () {}
        ,done: function(value, startDate, endDate){
            var startDateStr = startDate.year + '-' + startDate.month + '-' + startDate.date,
                endDateStr = endDate.year + '-' + endDate.month + '-' + endDate.date;
            $("input[name=date_type_week_start]").val(startDateStr)
            $("input[name=date_type_week_end]").val(endDateStr)
        }
    });
    // 日历初始化 - 按星期设置------------

    suitThead.on('click', '.confirm_suit_filter', function () {
        var cIndex = $(this).data('index'),
            allIndex = "all_" + cIndex,
            valObj,
            val,
            valType= 'input';
        if (cIndex === 'stock_mod') {
            valType = 'select';
        }

        valObj = $(valType + "[name=" + allIndex + "]");
        val = valObj.val();
        if(val === '') {
            ST.Util.showFailureMsg('输入不能为空');
            return false;
        }
        if(val < 0) {
            ST.Util.showFailureMsg('输入需大于 0');
            return false;
        }

        suitTbody.find('.show-sku').find(valType+"[name*=" + cIndex + "]").each(function(index, item){
            item.value = val
            if(cIndex === 'stock_mod') {
                changeStockMod(item)
            }
        });

        $(this).parent().parent().hide();

        updateSalePrice();
    }).on('click', '.reset_suit_filter', function () {
        $(this).parent().parent().hide();
    }).on('click', ".suit-setting-tool", function (e) {
        $(this).parent().find('.suit-setting-con').toggle()
    })

    suitTbody.on('blur', "input[name*=basic_price]", function () {
        formatMoney(this);

        updateSalePrice();
    }).on('blur', "input[name*=profit]", function () {
        formatMoney(this);

        updateSalePrice();
    }).on('change', "select[name*=stock_mod]", function () {
        changeStockMod(this)
    })

    // 切换库存mod
    function changeStockMod(obj) {
        var id = $(obj).data('id')
            ,val = $(obj).val()
            ,stockId = ".stock-setting-"+val+"-"+id
            ,stockModId = ".stock-mod-"+val+"-"+id
            ,totalStockId = ".total-stock-"+val+"-"+id;
        $(stockId).removeClass('hide');
        $(stockId).siblings().addClass('hide');
        $(stockModId).removeClass('hide');
        $(stockModId).siblings().addClass('hide');
        $(totalStockId).removeClass('hide');
        $(totalStockId).siblings().addClass('hide');
    }

    // 重新更新卖价
    function updateSalePrice() {
        var suits = suitTbody.find('tr');

        suits.each(function (index, suit) {
            var basicPrice = $(suit).find('input[name*=basic_price]').val()
                , profit =  $(suit).find('input[name*=profit]').val()
                , settlementType =  $(suit).find('input[name*=settlement_type]').val()
                , salePriceObj =  $(suit).find('.sale-price'),
                salePrice = '--';

            if(parseInt(settlementType) === 1) {
                if(basicPrice && profit) {
                    salePrice = parseFloat(basicPrice) + parseFloat(profit);
                    salePrice = salePrice.toFixed(2)
                }
                salePriceObj.text(salePrice)
            }
        })
    }

    // 对输入金额进行校验处理
    function formatMoney(obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");//清除"数字"和"."以外的字符
        obj.value = obj.value.replace(/^\./g, "");//验证第一个字符是数字而不是字符
        obj.value = obj.value.replace(/\.{2,}/g, ".");//只保留第一个.清除多余的
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
    }

    // 按钮保存
    $("#confirm_btn").on("click", function(e) {
        // 日期选择
        var dateRes = checkDateSelect()
        if(!dateRes.status) {
            ST.Util.showFailureMsg(dateRes.msg);
            return false;
        }

        // 价格库存设置
        var priceRes = checkPriceSet()
        if(!priceRes.status) {
            ST.Util.showFailureMsg(priceRes.msg);
            return false;
        }

        //抽佣验证
        var commissionRes = checkCommission();
        if(!commissionRes.status) {
            ST.Util.showFailureMsg(commissionRes.msg);
            return false;
        }

        var frm_data = $("#suit_form").serialize();
        ST.Util.showLoading('保存中');
        $.ajax({
            data: frm_data,
            dataType: 'json',
            type: 'post',
            url: SITEURL + 'ferry/admin/ferry/ajax_save_batch_price',
            success: function (result) {
                if (result.status) {
                    ST.Util.showSuccessMsg(result.msg);
                    setTimeout(function () {
                        ST.Util.responseDialog({},true);
                    }, 1500)
                } else {
                    ST.Util.showMsg(result.msg, 5, 1000);
                }
            }
        })
    })

    // 检查日期选择
    function checkDateSelect() {
        try {
            var dateType = $("input[name=date_type]:checked").val();
            if (dateType === 'week') {
                // 按星期选择
                var weekStart = $("input[name=date_type_week_start]").val(),
                    weekEnd = $("input[name=date_type_week_end]").val();
                if (!weekStart || !weekEnd) {
                    setTimeout(function () {
                        $('#price_date_range').click()
                    }, 1000);
                    throw  '请选择日期范围';
                }
            } else {
                var dateTypes = $("input[name*=date_type_date]"),
                    dates = [];
                dateTypes.each(function (index, item) {
                    dates.push(item.value)
                })
                if (dates.length === 0) {
                    throw '请选择报价日期';
                }
            }
            return {status: true, msg: ''}
        } catch (e) {
            return {status: false, msg: e}
        }
    }

    // 检查价格库存设置
    function checkPriceSet() {
        try {
            var skus = suitTbody.find('.show-sku');
            skus.each(function (index, item) {
                var settlementType = $(item).find('input[name*=settlement_type]').val(),
                    stockModObj = $(item).find('select[name*=stock_mod]')[0],
                    stockObj = $(item).find('input[name*=stock]')[0],
                    warningStockObj = $(item).find('input[name*=warning_stock]')[0],
                    totalStockObj = $(item).find('input[name*=total_stock]')[0],
                    stockMod = stockModObj.value,
                    stock = stockObj.value,
                    warningStock = warningStockObj.value,
                    totalStock = totalStockObj.value;

                if(parseInt(settlementType) === 2){
                    var priceObj = $(item).find('input[name*=basic_price]')[0],
                        price = priceObj.value;
                    if (price < 0 || price === '') {
                        priceObj.focus();
                        throw '售价必须大于等于 0';
                    }
                }else{
                    var basicPriceObj = $(item).find('input[name*=basic_price]')[0],
                        profitObj = $(item).find('input[name*=profit]')[0],
                        basicPrice = basicPriceObj.value,
                        profit = profitObj.value;
                    if (basicPrice < 0 || basicPrice === '') {
                        basicPriceObj.focus();
                        throw '底价必须大于等于 0';
                    }

                    if (profit < 0 || profit === '') {
                        profitObj.focus();
                        throw '利润必须大于等于 0';
                    }
                }

                if (stockMod === '1') {
                    if (stock < 0 || stock === '') {
                        stockObj.focus();
                        throw '库存必须大于等于 0';
                    }

                    if(warningStock){
                        if(warningStock < 0){
                            warningStockObj.focus();
                            throw '预警库存必须大于 0';
                        }

                        stock = parseInt(stock)
                        warningStock = parseInt(warningStock)
                        if(stock > 0 && warningStock >= stock) {
                            warningStockObj.focus();
                            throw '预警库存必须小于库存';
                        }
                    }
                }

                if(stockMod === '3'){
                    if (totalStock < 0) {
                        totalStockObj.focus();
                        throw '总库存必须大于 0';
                    }
                }
            })

            return {status: true, msg: ''}
        } catch (e) {
            return {status: false, msg: e}
        }
    }

    function settings_switch(obj,ticket_id,sku_id)
    {
        var type = obj.find('option:selected').val();
        if(type==='1'){
            $("[name='settlement_val["+ticket_id+"_"+sku_id+"]']").attr('placeholder','比例0-100').val('');
        }else{
            $("[name='settlement_val["+ticket_id+"_"+sku_id+"]']").attr('placeholder','固定金额').val('');
        }
    }
    function number_constraint(obj,ticket_id,sku_id)
    {
        var settings_type = $("[name='settlement_key["+ticket_id+"_"+sku_id+"]']").find('option:selected').val();
        var val = obj.val();
        if(settings_type==='1'){
            //this.value=this.value.replace(/\D/g,'')
            val = val.replace(/\D/g,'')
            if(val>100){
                val = 100
            }else if(val<0){
                val = 0
            }
        }else{
            //value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
            val = val.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
        }
        obj.val(val);
    }

    function checkCommission()
    {
        var msg = '';
        $('.commission-tbody tr').each(function(){
            var price = $(this).find('.basic_price').val();
            var settlement_key = $(this).find('.settlement_key').val();
            var settlement_val = $(this).find('.settlement_val').val();
            price = parseFloat(price);
            settlement_key = parseInt(settlement_key);
            settlement_val = parseFloat(settlement_val);
            if(settlement_val>0){
                if(settlement_key===2){
                    if(price<=settlement_val){
                        msg = '抽佣金额不得大于或等于卖价';
                        return false;
                    }
                }
            }
        });

        return {status: !msg, msg: msg}
    }
</script>
</html>